﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using dhsystem.produceForm;
using baseSet.importExport;

namespace dhsystem.orderForm
{
    public partial class FrmCustAddAdd : Form
    {
        custAddListBLL custAdd = new custAddListBLL();
        custAddListModel custAddListRow = new custAddListModel();
        custListBLL custList = new custListBLL();
        FrmCustAddList custAddList = new FrmCustAddList();
        BindingSource BindingSourcecustAddListAdd;
        string state;
        string custNum;
        public FrmCustAddAdd(BindingSource bindingSourcecustAddList, string _state ,string _custNum)
        {
            InitializeComponent();
            state = _state;
            BindingSourcecustAddListAdd = bindingSourcecustAddList;
            custNum = _custNum;
        }
        //打开窗口时绑定数据
        private void FrmCustAddAdd_Load(object sender, EventArgs e)
        {
            textcustNum.Text = custNum;
            textcustNum.Focus();
            //comboBoxCustNum.SelectedValue = custNum;
            //bindingSourcecustList.DataSource = custList.getcustListAll();
            //comboBoxCustNum.DataSource = bindingSourcecustList;
            //comboBoxCustNum.DisplayMember = "custName";
            //comboBoxCustNum.ValueMember = "custNum";
            if (state == "编辑")
            {
                textid.DataBindings.Add("text", BindingSourcecustAddListAdd, "recordid");
                textcustNum.DataBindings.Add("text", BindingSourcecustAddListAdd, "custNum");
                textaddress.DataBindings.Add("text", BindingSourcecustAddListAdd, "addres");
                textPhone.DataBindings.Add("text", BindingSourcecustAddListAdd, "phone");
                textLinkMan.DataBindings.Add("text", BindingSourcecustAddListAdd, "linkMan");
                textCreateUser.DataBindings.Add("text", BindingSourcecustAddListAdd, "createUser");
                textUpdateUser.DataBindings.Add("text", BindingSourcecustAddListAdd, "updateUser");
                textmark.DataBindings.Add("text", BindingSourcecustAddListAdd, "mark");
               
            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否新增客户送货地址资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        custAddListRow.recordid =Convert .ToInt32 ( textid.Text);
                        custAddListRow.custNum = textcustNum.Text ;
                        custAddListRow.addres = textaddress.Text;
                        custAddListRow.phone = textPhone.Text;
                        custAddListRow.linkMan = textLinkMan.Text;
                        custAddListRow.createDate = DateTime.Now;
                        custAddListRow.createUser = textCreateUser.Text;
                        custAddListRow.updateDate = DateTime.Now;
                        custAddListRow.updateUser = textUpdateUser.Text;
                        custAddListRow.mark = textmark.Text;
                        custAdd.custAddListInsert(custAddListRow);
                        BindingSourcecustAddListAdd.DataSource = custAdd.getcustAddListAll();
                        MessageBox.Show("新增成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改客户送货地址资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        custAddListRow.recordid = Convert.ToInt32(textid.Text);
                        custAddListRow.custNum = textcustNum.Text ;
                        custAddListRow.addres = textaddress.Text;
                        custAddListRow.phone = textPhone.Text;
                        custAddListRow.linkMan = textLinkMan.Text;
                        custAddListRow.createDate = DateTime.Now;
                        custAddListRow.createUser = textCreateUser.Text;
                        custAddListRow.updateDate = DateTime.Now;
                        custAddListRow.updateUser = textUpdateUser.Text;
                        custAddListRow.mark = textmark.Text;
                        custAdd.custAddListUpdate (custAddListRow);
                        BindingSourcecustAddListAdd.DataSource = custAdd.getcustAddListAll();
                        MessageBox.Show("修改成功");

                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttselectcustNum_Click(object sender, EventArgs e)
        {
            string field;
            field = "custNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.custList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textcustNum.Text  = productselect.pname;
            }
        }
    }
}
